
import java.awt.Color;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultEditorKit;
import javax.swing.text.DefaultHighlighter;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * text.java
 *
 * Created on May 20, 2014, 7:27:58 PM
 */
/**
 *
 * @author Jubin
 */
public class text extends javax.swing.JFrame {

    Hexcode h=new Hexcode();
    Preprocessor p=new Preprocessor();
    String[] code;
    /** Creates new form text */
    public text() {
        //initComponents();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane1.setViewportView(jTextPane1);
        jTextPane1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextPane1KeyReleased(evt);
            }
        });
        add(jScrollPane1);
        
        h.initHexcode();
        code=h.S;
        jTextPane1.getDocument().putProperty(DefaultEditorKit.EndOfLineStringProperty, "\n");
        
    }
    
    public void setColor(Color c,String s){
        try {
            StyledDocument doc = jTextPane1.getStyledDocument();
            javax.swing.text.Style style = jTextPane1.addStyle("", null);
            StyleConstants.setForeground(style, c);
            doc.insertString(doc.getLength(), s, style);
            jTextPane1.select(0, 10);
            jTextPane1.setSelectedTextColor(Color.YELLOW);
        } catch (BadLocationException ex) {
                System.out.println(ex);
        }
    }

    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jTextPane1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jTextPane1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextPane1FocusLost(evt);
            }
        });
        jTextPane1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTextPane1KeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTextPane1);

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(148, 148, 148)
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 275, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    private void jTextPane1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextPane1KeyReleased
            
        if(!doTextPaneEventHandle(evt))return;
        colorEditor();
        
    }//GEN-LAST:event_jTextPane1KeyReleased

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        try {
            highligher(Integer.parseInt(jTextField1.getText()));
            
        } catch (Exception e) {
        }
}//GEN-LAST:event_jTextField1ActionPerformed

    private void jTextPane1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextPane1FocusLost
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextPane1FocusLost

    String[] undo=new String[0xFFF];
    int[] caretPos=new int[0xFFF];
    int undoIndex=-1;
    public void undo(){
        if(undoIndex>0) 
                {
                    jTextPane1.setText(undo[--undoIndex]);
                    jTextPane1.setCaretPosition(caretPos[undoIndex]);   
                }
    }
    
    public void redo(){
        if(undoIndex < undo.length) 
                    jTextPane1.setText(undo[++undoIndex]);
                    jTextPane1.setCaretPosition(caretPos[undoIndex]);
    }
    
    public boolean doTextPaneEventHandle(java.awt.event.KeyEvent evt){
        boolean ok=false;
            if(evt.isControlDown()&&evt.getKeyCode()==90) //undo
            {
                undo();
                ok=true;
            }
            else if(evt.isControlDown()&&evt.getKeyCode()==89) // redo
            {
                   redo();
                   ok=true;
            }
            else if(evt.isControlDown()&&evt.getKeyCode()==65) // select all
            {     ok=false;
            }
            else if(evt.getKeyCode()==17){ //ctrl character
                    ok=false;
            }
            else {
                undo[undoIndex=(++undoIndex)&undo.length]=jTextPane1.getText();
                caretPos[undoIndex]=jTextPane1.getCaretPosition();
                ok=true;
            }
        return ok;
    }
    
    public void colorEditor(){
        String s = jTextPane1.getText();
        int pt=jTextPane1.getCaretPosition();
        jTextPane1.setText("");
        Color c=Color.black,tmpC=Color.black;
        //label marker
        boolean label=false;
        for (int i = s.length()-2; i >=0; i--) {
            if(s.charAt(i)==':')label=true;
            if(label&&(s.charAt(i)=='#'||s.charAt(i)=='.'||s.charAt(i)==';'||s.charAt(i)=='/'||s.charAt(i)=='\n'))
            {
                label=false;
                s=s.substring(0, i+1)+(char)(0x207)+s.substring(i+1);
            }
        }
        
        for (int i = 0; i < p.code.length; i++) {
            if(!p.code[i].equalsIgnoreCase(""))
            {   if(s.contains(p.code[i]))
                    s=s.replaceAll(p.code[i], (char)(0x200)+p.code[i]+(char)(0x201));
                if(s.contains(p.code[i].toLowerCase()))
                    s=s.replaceAll(p.code[i].toLowerCase(), (char)(0x200)+p.code[i].toLowerCase()+(char)(0x201));
            }
        }

        for (int i = 0; i < code.length; i++) {
            if(!code[i].equalsIgnoreCase(""))
            {   if(s.contains(code[i]))
                    s=s.replaceAll(code[i], (char)(0x205)+code[i]+(char)(0x206));
                if(s.contains(code[i].toLowerCase()))
                    s=s.replaceAll(code[i].toLowerCase(), (char)(0x205)+code[i].toLowerCase()+(char)(0x206));
            }
        }
        
        Color lastColor=new Color(0x000000);
        for (int i = 0,mode=0; i < s.length(); i++) {
            
            if(s.charAt(i)=='#'){c= new Color(0xCC0000);mode=3;}
            else if(s.charAt(i)=='.'){c= new Color(0xCC0000);mode=3;}
            else if(s.charAt(i)==';'){c= new Color(0xC0C0C0);mode=2;}
            else if(s.charAt(i)=='/'){c= new Color(0xC0C0C0);mode=2;}
            else if(s.charAt(i)==0x207){c= new Color(0x00CC66);mode=1;}
            else if(s.charAt(i)=='\n'){c= Color.black;mode=0;}
            
            if(mode<2)
            {
                if(s.charAt(i)==0x205)c= new Color(0x0066CC);
                else if(s.charAt(i)==0x206)c= new Color(0xCCCC00);
            }

            if(mode==3)
            {
                if(s.charAt(i)==0x200)c= new Color(0x660000);
                else if(s.charAt(i)==0x201)c= new Color(0xCC0000);
            }
            
            if(mode!=1)
                lastColor=c;
            
            //if(s.charAt(i)>='0'&&s.charAt(i)<='9')setColor(Color.orange, Character.toString(s.charAt(i)));
            if(s.charAt(i)!=0x205&&s.charAt(i)!=0x206&&s.charAt(i)!=0x207&&s.charAt(i)!=0x200&&s.charAt(i)!=0x201)
                setColor(c, Character.toString(s.charAt(i)));
            
            
            if(s.charAt(i)==':'){c= lastColor;}
        }
        jTextPane1.setCaretPosition(pt);
    }
    
    public void highligher(int row){
        
        int[] line=new int[1000];
        line[0]=0;
        for (int i = 0,lineNo=0; i < jTextPane1.getText().length(); i++) {
            if(jTextPane1.getText().charAt(i)=='\n'){
                line[++lineNo]=i;
            }
        }
        try {
            jTextPane1.getHighlighter().removeAllHighlights();
            jTextPane1.setCaretPosition(line[row]);
            jTextPane1.getHighlighter().addHighlight(line[row], line[row+1], new DefaultHighlighter.DefaultHighlightPainter(new Color(0xFF3333)));
        } catch (Exception ex) {
            System.err.println(ex);
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new text().setVisible(true);
                
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextPane jTextPane1;
    // End of variables declaration//GEN-END:variables
}
